package com.study.aop.cglib字节码增强;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

/**
 * 工厂
 *
 * @author chenhs
 * @date 2019/9/21
 */
public class FactoryUserServciceImpl {
	
	public static UserServiceImpl creatUserService() {
		
		/*目标类*/
		final UserServiceImpl userService = new UserServiceImpl();
		/*切面类*/
		final Myaspect myaspect = new Myaspect();
		/*核心类*/
		//org.springframework.cglib.proxy.Enhancer;  这里容易导包导错。
		Enhancer enhancer = new Enhancer();
		/*放入目标类*/
		enhancer.setSuperclass(userService.getClass());
		/*创建代理对象*/
		enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
			myaspect.befor();
			Object invoke = method.invoke(userService, objects);
			myaspect.after();
			return invoke;
		});
		return (UserServiceImpl) enhancer.create();
	}
}
